package bma.common.langutil.objectquery;

import java.util.ArrayList;
import java.util.List;

public class ObjectQueryReq {

	protected ObjectQueryReq parent;
	protected String name;
	protected String alias;
	protected boolean returnValue;

	protected List<ObjectQueryReq> props;

	public ObjectQueryReq() {
		super();
	}

	public ObjectQueryReq(ObjectQueryReq parent) {
		super();
		this.parent = parent;
	}

	public boolean isReturnValue() {		
		return returnValue;
	}

	public void setReturnValue(boolean returnValue) {
		this.returnValue = returnValue;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getKey() {
		if (this.alias != null && !this.alias.isEmpty())
			return this.alias;
		return this.name;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public List<ObjectQueryReq> getProps() {
		return props;
	}

	public void setProps(List<ObjectQueryReq> props) {
		this.props = props;
	}

	public boolean hasProps() {
		return this.props != null && !this.props.isEmpty();
	}

	public ObjectQueryReq node(String n) {
		if (this.props == null) {
			this.props = new ArrayList<ObjectQueryReq>();
		}
		for (ObjectQueryReq prop : this.props) {
			if (prop.getName().equals(n)) {
				return prop;
			}
		}
		ObjectQueryReq prop = new ObjectQueryReq(this);
		prop.setName(n);
		this.props.add(prop);
		return prop;
	}

	public String fullName() {
		String r = "";
		if (parent != null) {
			String n = parent.fullName();
			if (n != null) {
				r += parent.fullName() + ".";
			}
		}
		r += name;
		return r;
	}

	public List<String> getPropsName() {
		List<String> r = new ArrayList<String>(props != null ? props.size() : 0);
		for (ObjectQueryReq prop : this.props) {
			r.add(prop.getName());
		}
		return r;
	}
}
